<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/6/28
 * Time: 下午4:25
 */

class Quiz extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('mp_quiz_model');
    }

    public function getQuestions()
    {
        $data = $this->mp_quiz_model->getQuestions();
        $this->success($data);
    }

    public function getHusQuestion()
    {
        $fuid = $this->input->get('fuid');
        $sid = $this->input->get('sid');
        if (empty($sid) || empty($fuid)) $this->error();
        $data = $this->mp_quiz_model->getHusQuestion($fuid, $sid);
        $this->success($data);
    }

    public function makeq()
    {
        $rawdata = $this->input->raw_input_stream;
        $data = json_decode($rawdata, true);
        if (empty($data['uid']) || empty($data['ans'])) $this->error();

        $randstr = $this->mp_quiz_model->makeq($data['uid'], $data['ans']);
        $this->success(array('randstr' => $randstr));
    }

    public function startAnsq()
    {
        $uid = $this->input->get('uid');
        $fuid = $this->input->get('fuid');
        $sid = $this->input->get('sid');

        if (empty($uid)||empty($fuid)||empty($sid)) $this->error();
        $this->mp_quiz_model->saveOpenTime($uid, $fuid, $sid);
        $data = $this->mp_quiz_model->getUserInfo($fuid);
        $this->success($data);
    }

    public function showResults()
    {
        $rawdata = $this->input->raw_input_stream;
        $data = json_decode($rawdata,true);
        if (empty($data['husans'])||empty($data['uid'])||
            empty($data['fuid'])||empty($data['sid'])||empty($data['mpid']))
            $this->error();
//        $data['husans'] = "{\"fifa\":[{\"qno\":1,\"val\":\"1\"},{\"qno\":6,\"val\":\"2\"},{\"qno\":4,\"val\":\"3\"},{\"qno\":9,\"val\":\"4\"},{\"qno\":8,\"val\":\"1\"}],\"wife\":[{\"qno\":7,\"val\":\"4\"},{\"qno\":2,\"val\":\"3\"},{\"qno\":1,\"val\":\"2\"},{\"qno\":3,\"val\":\"1\"},{\"qno\":6,\"val\":\"4\"}]}";
//        $data['fuid'] = 1;
//        $data['uid'] = 1;
//        $data['sid'] = "55blQZ";

        $res = $this->mp_quiz_model->getResults($data['uid'], $data['fuid'], $data['sid'], $data['husans'],$data['mpid']);
//        echo '-------------------------------------';
//        echo '<pre>';
//        var_dump($res);
//        echo '</pre>';
        $this->success($res);

    }

    public function getPostUrl()
    {
        $fuid = $this->input->get('fuid');
        $uid = $this->input->get('uid');
        $sid = $this->input->get('sid');
        if (empty($fuid)||empty($uid)||empty($sid)) $this->error();
        $url = $this->mp_quiz_model->getPostUrl($uid,$fuid,$sid);
        $this->success(array('postUrl'=>$url));
    }

    public function saveNotifyFormid()
    {
        $uid = $this->input->get('uid');
        $formid = $this->input->get('formid');
        if(empty($formid)||empty($uid)) $this->error();
        $formid = $this->mp_quiz_model->saveNotifyFormid($uid,$formid);
        $this->success($formid);
    }

//    public function test()
//    {
//        $this->mp_quiz_model->createRandData();
//    }
}